package com.example.administrator.maizuofilm.cinemaschedule.adapter;

import java.util.List;

import org.w3c.dom.Text;

import com.example.administrator.maizuofilm.R;
import com.example.administrator.maizuofilm.bean.Filmlist2;
import com.example.administrator.maizuofilm.bean.ForetellList;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;


import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class FilmListAdapter extends BaseAdapter {

    private List<Filmlist2> list;
    private LayoutInflater layout;

    public FilmListAdapter(Context context, List<Filmlist2> list) {
        this.list = list;
        layout = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = layout.inflate(R.layout.item_film_time, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ForetellList f = list.get(position).getFlist().get(0);
        String time = f.getShowTime();
        int time1 = Integer.parseInt(time.substring(0, 2));
        int time2 = Integer.parseInt(time.substring(2, time.length()));
        int apart1 = f.getDuration() / 60;
        int apart2 = f.getDuration() % 60;
        if (apart2 >= 60) {
            apart1 = apart1 + 1;
            apart2 = apart2 % 60;
        }
        if (apart1 > 24) {
            apart1 = apart1 % 24;
        }
        int ap1 = time1 + apart1;
        int ap2 = time2 + apart2;
        int price = Integer.parseInt(f.getPrice()) / 100;
        int market = Integer.parseInt(f.getMarketPrice()) / 100;
        String dd = f.getLanguage().substring(0, 2);
        holder.tv_time.setText(time1 + ":" + time2);
        holder.tv_apart.setText("预计" + ap1 + ":" + ap2 + "散场");
        holder.tv_type.setText(dd + f.getDimensional());
        holder.tv_hall.setText(f.getHallName());
        holder.tv_price.setText("￥" + price);
        if (f.getForetellId() != null && !"".equals(f.getForetellId())) {
            holder.tv_price.setTextColor(Color.RED);
        } else {
            holder.tv_price.setTextColor(Color.BLACK);
        }

        holder.tv_market.setText("￥" + market);
        holder.tv_market.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.film_show_time)
        TextView tv_time;
        @ViewInject(R.id.film_show_apart)
        TextView tv_apart;
        @ViewInject(R.id.film_show_type)
        TextView tv_type;
        @ViewInject(R.id.film_show_hall)
        TextView tv_hall;
        @ViewInject(R.id.film_show_price)
        TextView tv_price;
        @ViewInject(R.id.film_show_market)
        TextView tv_market;
    }
}
